package cn.uncode.mq.server.handlers;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.uncode.mq.network.Message;
import cn.uncode.mq.network.Topic;
import cn.uncode.mq.server.RequestHandler;
import cn.uncode.mq.util.DataUtils;

/**
 * MQ消费处理器
 *
 * @author : juny.ye
 */
public class FetchRequestHandler implements RequestHandler {
	
	private final static Logger LOGGER = LoggerFactory.getLogger(FetchRequestHandler.class);
	
	@Override
	public Message handler(Message request) {
		List<Topic> result = new ArrayList<Topic>();
		List<Topic> topics = (List<Topic>) DataUtils.deserialize(request.getBody());
		if(topics != null){
			for(Topic topic:topics){
				result.add(ProducerRequestHandler.maps.get(topic.getTopic()));
			}
		}
		Message response = Message.newResponseMessage();
		response.setSeqId(request.getSeqId());
		response.setBody(DataUtils.serialize(result));
		LOGGER.info("Fetch request handler, message:"+result.toString());
		return response;
	}

	

	
}
